/*
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.dspl.validate;

import com.google.common.collect.ImmutableList;
import com.google.dspl.model.Concept;
import com.google.dspl.model.Dataset;
import com.google.dspl.model.DatasetItem;
import com.google.dspl.model.Slice;
import com.google.dspl.model.Table;
import com.google.dspl.model.Topic;
import com.google.dspl.validate.validator.Validator;
import com.google.dspl.validate.violation.Violation;

import java.util.Collection;
import java.util.List;

/**
 * Runs validation rules on given inputs.
 *
 * @author Shardul Deo
 */
public final class DatasetValidation {

  private final ImmutableList<Validator<Dataset>> datasetValidators;
  private final ImmutableList<Validator<Topic>> topicValidators;
  private final ImmutableList<Validator<Concept>> conceptValidators;
  private final ImmutableList<Validator<Slice>> sliceValidators;
  private final ImmutableList<Validator<Table>> tableValidators;

  DatasetValidation(
      Collection<Validator<Dataset>> datasetValidators,
      Collection<Validator<Topic>> topicValidators,
      Collection<Validator<Concept>> conceptValidators,
      Collection<Validator<Slice>> sliceValidators,
      Collection<Validator<Table>> tableValidators) {
    this.datasetValidators = ImmutableList.copyOf(datasetValidators);
    this.topicValidators = ImmutableList.copyOf(topicValidators);
    this.conceptValidators = ImmutableList.copyOf(conceptValidators);
    this.sliceValidators = ImmutableList.copyOf(sliceValidators);
    this.tableValidators = ImmutableList.copyOf(tableValidators);
  }

  private <T extends DatasetItem<T>> List<Violation> validate(
      T item, List<Validator<T>> validators, ValidationContext context) {
    ImmutableList.Builder<Violation> violations = ImmutableList.builder();
    for (Validator<T> validator : validators) {
      violations.addAll(validator.validate(item, context));
    }
    return violations.build();
  }

  /**
   * Runs the appropriate validation rules on the given item and adds any validation errors found to
   * the context.
   * <p>
   * Note that if item is a {@link Dataset}, it will only run validations for dataset metadata and
   * not all the validations for every element in the dataset. For doing an entire validation of a
   * dataset, use {@link #validateDataset(Dataset, ValidationContext)}.
   *
   * @param item Must be a {@link Dataset}, {@link Topic}, {@link Concept}, {@link Slice} or
   *        {@link Table}.
   * @param context This context must contain the dataset being validated in addition to other
   *        datasets that might be referred to.
   */
  public List<Violation> validateItem(DatasetItem<?> item, ValidationContext context) {
    ImmutableList.Builder<Violation> violations = ImmutableList.builder();
    if (item instanceof Dataset) {
      violations.addAll(validate((Dataset) item, datasetValidators, context));
    } else if (item instanceof Topic) {
      violations.addAll(validate((Topic) item, topicValidators, context));
    } else if (item instanceof Concept) {
      violations.addAll(validate((Concept) item, conceptValidators, context));
    } else if (item instanceof Slice) {
      violations.addAll(validate((Slice) item, sliceValidators, context));
    } else if (item instanceof Table) {
      violations.addAll(validate((Table) item, tableValidators, context));
    } else {
      throw new IllegalArgumentException(
          "Item to be validated can must be a Dataset, Topic, Concept, Slice or Table.");
    }
    return violations.build();
  }

  /**
   * Validates an entire dataset, including all the items in it and adds any validation errors found
   * to the context.
   *
   * @param context This context must contain datasets that are referred to by the dataset being
   *        validated.
   */
  public List<Violation> validateDataset(Dataset dataset, ValidationContext context) {
    ImmutableList.Builder<Violation> violations = ImmutableList.builder();
    violations.addAll(validateItem(dataset, context));
    for (Topic topic : dataset.getTopics()) {
      violations.addAll(validateItem(topic, context));
    }
    for (Concept concept : dataset.getConcepts()) {
      violations.addAll(validateItem(concept, context));
    }
    for (Slice slice : dataset.getSlices()) {
      violations.addAll(validateItem(slice, context));
    }
    for (Table table : dataset.getTables()) {
      violations.addAll(validateItem(table, context));
    }
    return violations.build();
  }
}
